package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.DomainEntity;
import com.ruoyi.system.domain.dto.DomainDTO;
import com.ruoyi.system.domain.dto.DomainLandingDTO;
import com.ruoyi.system.domain.dto.DomainLinkDTO;
import com.ruoyi.system.mapper.DomainDao;
import com.ruoyi.system.service.DomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class DomainServiceImpl implements DomainService {

	@Autowired
	private DomainDao domainDao;

	@Override
	public DomainEntity queryByDomain(String domain) {
		return domainDao.queryByDomain(domain);
	}

	@Override
	public List<DomainEntity> queryDomainListByParentId(long domainParentId) {
		return domainDao.queryDomainListByParentId(domainParentId);
	}

	@Override
	public List<DomainEntity> queryAllDomainList() {
		return domainDao.queryAllDomainList();
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int insertDomain(DomainLinkDTO domainLink) {
		String domain = domainLink.getDomain();
		int status = domainLink.getStatus();
		
		// 主域名
		long domainId = setDomain(domain, status, 0L, 0);
		log.info("domain--------{}", domain);

		List<DomainDTO> links = domainLink.getLinks();
		for (DomainDTO link : links) {
			long linkId = setDomain(link.getDomain(), link.getStatus(), domainId, 0);
			List<DomainLandingDTO> domainLandingPages = link.getPages();
			for (DomainLandingDTO domainLandingPage : domainLandingPages) {
				setDomain(domainLandingPage.getDomain(), domainLandingPage.getStatus(), linkId, 1);
			}
		}

		return 1;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int updateDomain(DomainLinkDTO domainLink) {
		String domain = domainLink.getDomain();
		int status = domainLink.getStatus();

		// 主域名
		update(domainLink.getId(), domain, status);
		List<DomainDTO> links = domainLink.getLinks();
		for (DomainDTO link : links) {
			if(link.getId() != null && !String.valueOf(link.getId()).isEmpty()) {
				update(link.getId(), link.getDomain(), link.getStatus());
				editDomain(link, link.getId());
			}else{
				long domainId = setDomain(link.getDomain(), link.getStatus(), domainLink.getId(), 0);
				editDomain(link, domainId);
			}

		}

		return 1;
	}

	private void editDomain(DomainDTO link, Long domainId) {
		List<DomainLandingDTO> domainLandingPages = link.getPages();
		if(domainLandingPages!=null && !domainLandingPages.isEmpty()){
			for (DomainLandingDTO domainLandingPage : domainLandingPages) {
				if(domainLandingPage.getId() != null && !String.valueOf(domainLandingPage.getId()).isEmpty()) {
					update(domainLandingPage.getId(), domainLandingPage.getDomain(), domainLandingPage.getStatus());
				}else{
					setDomain(domainLandingPage.getDomain(), domainLandingPage.getStatus(), domainId, 1);
				}
			}
		}
	}

	@Override
	public DomainEntity selectDomainById(Long domainId)
	{
		return domainDao.selectDomainById(domainId);
	}

	private long setDomain(String domain, int status, long parentId, int isLanddingPage) {
		DomainEntity domainEntity = new DomainEntity();
		domainEntity.setDomain(domain);
		domainEntity.setStatus(status);
		domainEntity.setParentId(parentId);
		if(isLanddingPage == 1){
			domainEntity.setIsLuodi(1);
		}
		int rows = domainDao.insertDomain(domainEntity);
		return domainEntity.getId();
	}

	private long update(Long domainId, String domainName, int status) {
		DomainEntity domain = domainDao.selectDomainById(domainId);
		domain.setDomain(domainName);
		domain.setStatus(status);
		int rows = domainDao.updateDomain(domain);
		return rows;
	}


}
